// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
const {
	giveMsg,
	giveCode,
	result
} = require('shop_common');

const {
	secret,
	getName
} = require('demo1')

module.exports = {
	_before: function() { // 通用预处理器
		this.startTime = Date.now()
		this.params = this.getParams()[0]

		//this.params = this.getParams()
		//this.params = JSON.parse(this.getHttpInfo().body)
	},

	async get() {
		//return this.params
		let {
			num
		} = this.params;
		let res = await db.collection("opendb-mall-categories").limit(num).get();
		let result = {
			errCode: 0,
			errMsg: "查询成功",
			data: res.data
		};
		return result;
	},

	async getyjy() {
		let name = getName("小明")
		result = {
			name,
			secret
		}
		return result;
	},

	async get_opendb_city_china() {
		let res = await db.collection("opendb-city-china").get({getTree:true});
		
		/* let res = await db.collection("opendb-city-china").get();
		let resdata = res.data;
		for (let i = 0; i < resdata.length - 1; i++) {
			if ("parent_code" in resdata[i]) {
				let pitem = resdata.find(item => item.code === resdata[i].parent_code);
				if (pitem) {
					await db.collection('opendb-city-china').doc(resdata[i]._id).update({
						"parent_code": pitem._id
					});
				}
			}
		} */
		
		return result(200, "success", res.data);
	},

	add: async function() {

	},

	_after(error, result) {
		if (error) {
			throw error
		}
		result.timeCost = Date.now() - this.startTime
		//result.yjy = "我胜利了" 

		return result
	}

}